<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="logo-container">
        <div class="logo">CNN-IT®</div>
      </div>

      <div class="title-container">
        <h3 class="title">欢迎回来</h3>
        <p class="subtitle">登录您的项目管理面板账户</p>
      </div>

      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button :loading="loading" type="primary" class="login-button" @click.native.prevent="handleLogin">登录</el-button>

    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur' }],
        password: [{ required: true, trigger: 'blur' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/management' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #f8f9ff;
$light_gray: #333;
$cursor: #333;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 50px;
    width: 100%;

    input {
      background: #fff;
      border: 1px solid #e0e0e0;
      -webkit-appearance: none;
      border-radius: 8px;
      padding: 12px 15px 12px 45px;
      color: $light_gray;
      height: 50px;
      caret-color: $cursor;
      font-size: 14px;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #fff inset !important;
        -webkit-text-fill-color: $cursor !important;
      }

      &:focus {
        border-color: #7c3aed;
        outline: none;
        box-shadow: 0 0 0 2px rgba(124, 58, 237, 0.1);
      }
    }
  }

  .el-form-item {
    border: none;
    background: transparent;
    border-radius: 0;
    color: #333;
    margin-bottom: 20px;
  }
}
</style>

<style lang="scss" scoped>
$bg: linear-gradient(135deg, #f8f9ff 0%, #ffffff 100%);
$dark_gray: #666;
$light_gray: #333;

.login-container {
  min-height: 100vh;
  width: 100%;
  background: $bg;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;

  .login-form {
    position: relative;
    width: 400px;
    max-width: 90%;
    padding: 40px;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 16px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
    backdrop-filter: blur(10px);
  }

  .logo-container {
    text-align: center;
    margin-bottom: 30px;

    .logo {
      font-size: 32px;
      font-weight: bold;
      color: #dc2626;
      font-family: 'Arial', sans-serif;
      letter-spacing: 1px;
    }
  }

  .tips {
    font-size: 14px;
    color: #666;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    position: absolute;
    left: 15px;
    top: 50%;
    transform: translateY(-50%);
    color: $dark_gray;
    z-index: 10;
    font-size: 18px;
  }

  .title-container {
    position: relative;
    text-align: center;
    margin-bottom: 40px;

    .title {
      font-size: 28px;
      color: $light_gray;
      margin: 0 0 8px 0;
      font-weight: 600;
    }

    .subtitle {
      font-size: 14px;
      color: $dark_gray;
      margin: 0;
      font-weight: 400;
    }
  }

  .show-pwd {
    position: absolute;
    right: 15px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 18px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
    z-index: 10;
  }

  .login-button {
    width: 100%;
    height: 50px;
    background: linear-gradient(135deg, #7c3aed 0%, #a855f7 100%);
    border: none;
    border-radius: 8px;
    color: white;
    font-size: 16px;
    font-weight: 600;
    margin-top: 20px;
    transition: all 0.3s ease;

    &:hover {
      background: linear-gradient(135deg, #6d28d9 0%, #9333ea 100%);
      transform: translateY(-1px);
      box-shadow: 0 4px 12px rgba(124, 58, 237, 0.3);
    }

    &:active {
      transform: translateY(0);
    }
  }
}
</style>
